<?php

namespace app\common\model;


use think\Exception;
use think\facade\Cache;

class MallSpecsValue extends Base
{

    protected $name = "mall_specs_value";

    protected $deleteTime = "delete_time";


    public function getStatusList()
    {
        return ['0' => '禁用', '1' => '启用',];
    }

    /**
     * @param array $ids
     * @return array|string
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSpecsValueByIdValue(array $ids = [])
    {
        $row = $this
            ->field("id,specs_id,title")
            ->where('id', 'in', $ids)
            ->select()
            ->toArray();
        if (empty($row)) {
            return "-";
        }
        //获取所有规格
        $specs_ids = array_column($row, "specs_id");
        if (empty($specs_ids)) {
            return "-";
        }
        try {
            $specs = (new MallSpecs())->getSpecsByIdsValue($specs_ids);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (empty($specs)) {
            return "-";
        }
        $specs_value = [];
        foreach ($specs as $v) {
            $specs_value[$v['id']] = $v;
        }
        $lists = [];
        foreach ($row as $k => $value) {
            $lists[$k] = $value;
            $lists[$k]['value'] = $specs_value[$value['specs_id']]['title'] . ":" . $value['title'];
        }
        return $lists;

    }

    /**
     * @param array $ids
     * @param string $flagValue
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetailByGoodsSku(array $ids = [], string $flagValue = ''): array
    {
        $specsValue = array_keys($ids);
        $news = [];
        $specsValueIds = [];
        foreach ($specsValue as $v) {
            $specsValueKey = explode('-', $v);
            foreach ($specsValueKey as $kk => $vv) {
                $news[$kk][] = $vv;
                $specsValueIds[] = $vv;
            }
        }
        $specsValueIds = array_unique($specsValueIds);
        $specsValues = $this->getNormalInIds($specsValueIds);
        $result = [];
        $flagValue = explode('-',$flagValue);
        foreach ($news as $k => $v) {
            $newValue = array_unique($v);
            $list = [];

            foreach ($newValue as $vv) {
                $list[] = [
                    "id" => $vv,
                    "name" => $specsValues[$vv]['title'],
                    "flag" => in_array($vv,$flagValue) ? 1 : 0,
                ];
            }
            $result[$k] = [
                "name" => $specsValues[$newValue[0]]['specs_title'],
                "list" => $list
            ];
        }
        return $result;
    }

    /**
     * @param array $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getNormalInIds(array $ids = []): array
    {
        try {
            $result = $this->whereIn('id', $ids)
                ->where('status', 1)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        //获取规格值
        try {
            $specs = (new MallSpecs())->getSpecsByIdsValue(array_column($result, 'specs_id'));
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (empty($specs)) {
            throw new Exception("值不存在");
        }
        $specsName = array_column($specs, 'title', 'id');
        $res = [];
        foreach ($result as $k => $v) {
            $res[$v['id']] = [
                "title" => $v['title'],
                "specs_title" => $specsName[$v['specs_id']] ?? ""
            ];
        }
        return $res;
    }

    /**
     * 获取规格值
     * 返回 红色 s
     * @param string $ids
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSpecsValueStringIds(string $ids = ""){
        if (empty($ids)) {
            return '';
        }
        $key = "getSpecsValueStr_".$ids;
        $getCache = Cache::get($key);
        if($getCache){
            return $getCache;
        }else {
            $ids = explode("-", $ids);
            $res = $this->where('id', 'in', $ids)
                ->select()->toArray();
            $arr = array_column($res, 'title');
            $str= implode(' ', $arr);
            Cache::set($key,$str,72000);
            return $str;
        }
    }
}